package cluster

import (
	"context"
	"kms-backend/config"
	"kms-backend/utils/logs"
	"net/http"

	"github.com/gin-gonic/gin"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func Delete(r *gin.Context) {
	logs.Debug(nil, "删除集群")
	// 1. 获取集群Id
	// 前端传递参数clusterId
	clusterId := r.Query("clusterId")
	returnData := config.ReturnData{}
	// 2. 删除集群，即删除Secrect
	err := config.InClusterClientSet.CoreV1().Secrets(config.MetadataNamespace).Delete(context.TODO(), clusterId, metav1.DeleteOptions{})
	if err != nil {
		// 删除失败
		logs.Error(nil, "集群删除失败: "+err.Error())
		msg := "删除集群失败: " + err.Error()
		returnData.Message = msg
		returnData.Status = 400
	} else {
		// 删除成功
		logs.Info(map[string]interface{}{"当前被删除集群Id": clusterId}, "集群删除成功")
		returnData.Message = "集群删除成功"
		returnData.Status = 200
		// 删除初始化时获取的集群信息中现在删除的集群
		delete(config.ClusterKubeConfig, clusterId)
		// 测试输出config.ClusterKubeConfig的信息
		// fmt.Print(config.ClusterKubeConfig)
	}
	r.JSON(http.StatusOK, returnData)

}
